import * as userService from  '../services/users'

export default {
  namespace: 'users',
  state: {
    list: [],
    total: 0,
    page: 0
  },
  reducers: {
    save (state,{ payload: {data:list, total, page} }) {
      return { ...state, list, total, page }
    },
    search (state) {
      return { ...state }
    }
  },
  effect: {
    *fetch ({ payload: { page }}, { call, put } ) {
        const { data,headers } = yield call(userService.fetch, {page})
        yield put({
          type: 'save',
          payload: {
            data,
            total: headers['x-total-count'],
            page: parseInt(page, 10)
          }
        })
    },
    *create({payload: values}, {call,put}) {
      yield call(userService.create, values)
    },
    *patch ({payload: {id,values}},{call,put}) {
      yield call(userService.patch,{id,values})
      yield put({type: 'reload'})
    },
    *remove({id},{call,put}){
      yield call(userService.remove,id)
      yield put({type: 'reload'})
    },
    *reload(action,{put,select}) {
      const page = yield select(state => state.user.page)
      yield put({type: 'fetch',payload: {page}})
    }
  },
  subscription: {

  }
}
